<?php

class SiteController extends Controller {

    /**
     * Declares class-based actions.
     */
    public function actions() {
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xFFFFFF,
            ),
            // page action renders "static" pages stored under 'protected/views/site/pages'
            // They can be accessed via: index.php?r=site/page&view=FileName
            'page' => array(
                'class' => 'CViewAction',
            ),
        );
    }

    public function actionIndex() {
        $slide = Slide::model()->findAll(array(
            'condition' => 't.active=:active',
            'params' => array(':active' => 1),
            'order' => ' t.order ASC '
                ));
        $cate = Category::model()->findAll(array(
            'condition' => 't.active=:active AND t.parent=:parent AND t.show_index=:show_index',
            'params' => array(':active' => 1, ':parent' => '0', ':show_index' => 1),
            'order' => ' t.order_index ASC ',
            'limit' => 3
                ));
        $product = array();
        foreach ($cate as $key => $value) {
            $product[$key] = $this->getProductByCategory($cate[$key]['id']);
        }
        //echo ($cate[1]['id']);die;
        $parent_cate = MyFunctionCustom::getProductByCategoryParent($cate[0]['id']);
        $this->render('index', array(
            'slide' => $slide,
            'product' => $product,
            'cate' => $cate,
            'parents' => $parent_cate
        ));
    }

    public static function getProductByCategory($id) {

        $product_criteria = new CDbCriteria();
        $product_criteria->select = 't.*';
        $product_criteria->with = 'category';
        $product_criteria->join = 'JOIN categories c on c.id= t.category_id';
        $product_criteria->condition = 't.active=:active AND t.category_id=:id AND c.active=:pactive';
        $product_criteria->params = array(':active' => 1, ':id' => $id, ':pactive' => 1);
        $product = Product::model()->findAll($product_criteria);
        return $product;
    }

    public function actionError() {
        if ($error = Yii::app()->errorHandler->error) {
            if (Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }

    /**
     * Displays the contact page
     */
    public function actionContact() {
        $model = new ContactForm;
        /* if (isset($_POST['ContactForm'])) {
          $model->attributes = $_POST['ContactForm'];
          if ($model->validate()) {
          $info = $_POST['ContactForm'];
          $message = new YiiMailMessage;
          $message->view = "email";
          $params = array(
          'info' => $info);
          $message->subject = 'Thông tin liên hệ';
          $message->setBody($params, 'text/html');
          $message->addTo('contact@phamgiavan.com');
          $message->addCC('nghuytap@gmail.com');
          $message->from = $info['email'];
          $result = Yii::app()->mail->send($message);
          //Yii::app()->user->setFlash('contact', 'Cảm ơn bạn đã liên hệ với chúng tôi. Chúng tôi sẽ liên lạc với bạn trong thời gian sớm nhất');
          //$this->refresh();
          }
          } */
        //$this->readXml();
        $model = new ContactForm;
        if (isset($_POST['ContactForm'])) {
            $model->attributes = $_POST['ContactForm'];
            $message = new YiiMailMessage;
            $message->view = "email";
            $info = $_POST['ContactForm'];
            $params = array('info' => $info);
            $message->subject = $model->subject;
            $message->setBody($params, 'text/html');
	    $message->from = $model->email;
            $message->addTo('sales@phamgiavan.com');
            $message->addCC('pgvtech@gmail.com');
            $result = Yii::app()->mail->send($message);
	    $this->render('contact',array('model'=>$model,'result' => $result));
        }
        $this->render('contact', array('model' => $model));
    }

    /**
     * Displays the login page
     */
    public function actionLogin() {
        $model = new LoginForm;

        // if it is ajax validation request
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        // collect user input data
        if (isset($_POST['LoginForm'])) {
            $model->attributes = $_POST['LoginForm'];
            // validate user input and redirect to the previous page if valid
            if ($model->validate() && $model->login())
                $this->redirect(Yii::app()->user->returnUrl);
        }
        // display the login form
        $this->render('login', array('model' => $model));
    }

    /**
     * Logs out the current user and redirect to homepage.
     */
    public function actionLogout() {
        Yii::app()->user->logout();
        $this->redirect(Yii::app()->homeUrl);
    }
}